package com.ljf.admin.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.admin.POJO.SysRole;
import com.ljf.admin.POJO.SysUser;
import com.ljf.admin.exception.LjfSystemException;
import com.ljf.admin.mapper.SysUserMapper;
import com.ljf.admin.service.SysRoleService;
import com.ljf.admin.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @Auther: lijinfeng
 * @Date: 2022/4/21
 * @Description 描述:
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper userMapper;

    @Override
    public boolean saveOrUpdateBatch(Collection<SysUser> entityList) {
        int i = userMapper.batchInsert((List<SysUser>) entityList);
        return i > 0;
    }

    @Resource
    private SysRoleService sysRoleService;

    @Override
    public int updateBatch(List<SysUser> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<SysUser> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<SysUser> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
//    @Cacheable(value = "user=100", key = "#root.targetClass+ ':'+methodName +':'+ #username")
    public SysUser selectUserByUserName(String username) {
        SysUser sysUser = userMapper.selectUserByUserName(username);
        if (ObjectUtils.isNull(sysUser)) {
            log.error("用户：{} 不存在",username);
            throw new LjfSystemException("用户：" + username + " 不存在");
        }
        List<SysRole> sysRoles = sysRoleService.selectRolesByUserId(sysUser.getId());
        sysUser.setRoleList(sysRoles);
        return sysUser;
    }
}
